package com.ikun.controller;

import com.ikun.pojo.Article;
import com.ikun.pojo.PageBean;
import com.ikun.pojo.Result;
import com.ikun.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

//@CrossOrigin
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @PostMapping
    public Result add(@RequestBody @Validated(Article.Add.class) Article article) {
        articleService.add(article);
        return Result.success();
    }


    @GetMapping
    public Result<PageBean<Article>> list(Integer pageNum, Integer pageSize, @RequestParam(required = false) Integer categoryId, @RequestParam(required = false) String state) {


        PageBean<Article> p = articleService.list(pageNum, pageSize, categoryId, state);
        return Result.success(p);
    }

    @GetMapping("/detail")
    public Result<Article> detail(Integer id) {
        Article article = articleService.findById(id);
        return Result.success(article);

    }

    @PutMapping
    public Result update(@RequestBody @Validated(Article.Update.class) Article article) {
        articleService.update(article);
        return Result.success();

    }


    @DeleteMapping
    public Result delete(@Validated(Article.Delete.class) Integer id){

        articleService.delete(id);
        return Result.success();
    }

}
